<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
    function add_new_category()
    {
        var url = 'add_category'
        var ps  = 'new_category='+$F('new_category')
        new Ajax.Updater('news_categories',
                          url,
                          {
                              parameters: ps,
                              insertion:Insertion.Top,
                              evalScripts:true
                          }
                        );
    }
    
    function insert_file_from_iframe(href,title,type,id)
    {
        if (type != "img") {
            alert('Sorry only Image files are allowed');
            return false;
        }
        $('news_file_id').value = id;

        Element.update('img_placeholder',"<img src='<?php get_installed_path() ?>/img.php?src="+href+"&h=100' id='news_image' align='absmiddle' />")

    }
    
// ]]>
</script>
<style type="text/css" media="screen">
/* <![CDATA[ */
    #news_categories {
        height: 75px;
        overflow:auto;
    }
    a.active {
        background:  red;
        color: #fff;
        font-weight: bold;
    }
/* ]]> */
</style>


<?php include load_template('_subnav.php') ?>
<h1 id='top_edit'><?php if($news->is_loaded()) {echo "Editing";} else {echo"Adding";} ?> News</h1>

<div id='autosave' style='position:absolute'></div>
<?php echo  error_messages_for('news') ?>
<?php echo  form_tag('modify',array('validation'=>'true','id'=>'news_authoring')) ?>
<?php echo  hidden_field('news','id') ?>
<?php echo  hidden_field_tag('go_back_to',$go_back_to) ?>




<?php if($news->is_loaded()) { ?>
    <p>[ <a href="#bottom_news_preview" onclick="new Effect.ScrollTo('bottom_news_preview');return false;">Preview &darr;</a> ]</p>
<?php } ?>

<div class='section'> 

    <table border="0" cellspacing="5" cellpadding="5" width='100%'>
        <tr>
            <td>
                <label for='news_title'><span style='color:red'>*</span> Title</label>
                <?php echo  text_field('news','title',array('alt'=>'blank','emsg'=>'Title must not be blank','size'=>'50','style'=>'width:99%')) ?>       

                <label for='news_source'>Source (optional)</label>
                <?php echo  text_field('news','source',array('size'=>35)) ?>     

                <label for='news_file_id'>Image (optional)</label>
                <div style='border:1px solid #aaa;padding:5px'>Image when shown is a thumbnail. It will and can be resized dynamically.<br /> 
                <?php
                $img_id = '';
                    if ( $news->is_loaded() )
                    {
                        $img_id = $news->get('file_id');
                        
                    }
                    if ( !empty($img_id) )
                    {
                        $path = get_installed_path();
                        use_helper('admin/files');
                        $path_to_file = public_path_to_file($img_id);
                        echo "<b>Current Image:</b><br /><span id='img_placeholder'>
                                <img src='$path/img.php?src=$path/$path_to_file&h=100' id='news_image' align='absmiddle' />
                             </span>";                                               
                    } else {
                        echo "<span id='img_placeholder'></span>";                        
                    }
                                                                                                       
                    echo "<a href='#'                     onclick=\"Effect.toggle('image_picker','blind');Element.activate('image_picker_link',!$('image_picker_link')['active'])\" 
                    id='image_picker_link' 
                    activetext=\"I'm Done Selecting\" 
                    activeclass='active'>Change Image</a>";
                    echo hidden_field_tag('news[file_id]',$img_id, array('id'=>'news_file_id'));
                ?>
                <iframe 
                   frameborder="0" 
                   id="image_picker" 
                   scrolling="auto" 
                   src="<?php echo get_installed_path()?>/admin/files/browse?files_filter_by=image"
                   width="99%" 
                   height="300"  style='display:none;background:#fff;border:1px solid #bbb;margin-top:5px'></iframe>
                 </div>
                <label for='news_content'>Story</label>
                <?php echo  text_area('news','content',array('size'=>'50x30','style'=>'width:99%')) ?>
                <?php create_editor_instance('news_content',array('more_link'=>true)) ?>

                <p style='text-align:center;padding:5px;'><?php echo  submit_tag('Save and Continue Editing') ?> <?php echo  submit_tag('Save') ?> 
                    <?php if (!isset($news_item['status']) || $news_item['status'] == 0 ) { echo submit_tag('Publish'); }?></p>            
            </td>
            <td width='20%'> 
                    <fieldset>
                        <legend>Post Status</legend>
                        <?php $status = isset($news_item['status']) ? $news_item['status'] : 0 ?>
                        <?php echo  select_tag('news[status]',
                            options_for_select(array('Draft','Publish'),$status)) ?>
                    </fieldset>

                    <fieldset>
                        <legend>Categories</legend>
                    <div id='add_new_cat'>
                        <input type='text' name='new_category' id='new_category' size='10'> 
                        <?php echo  button_tag('Add',array('onclick'=>'add_new_category();')) ?>

                    </div>
                    <div id='news_categories'>
                        <?php 
                        foreach($all_categories as $k=>$v) {
                            if ( in_array($k, $news_item_categories) )
                            {
                                $checked = true;            
                            } else {
                                $checked = false;
                            }  
                            echo "<label for='c_{$k}'>".check_box_tag('news[categories][]',$k,$checked,array('id'=>"c_{$k}"))." $v</label>";
                        }                                        
                        ?>
                    </div>
                </fieldset>                        

                <fieldset>
                    <legend>Post Slug</legend>
                    <?php echo  text_field('news','index_name') ?>
                </fieldset>

                <fieldset>
                    <legend>Post Timestamp</legend>
                    <label for="edit_timestamp"><?php echo  check_box_tag('edit_timestamp',1,false,array('id'=>'edit_timestamp')) ?> Edit timestamp?</label>
                    <?php 
                    if($news_item){
                        $news_publish_date = date_to_getdate($news_item['publish_date']);
                    } else {
                        $news_publish_date = array();
                    }
                    ?>
                    <?php echo  select_date($news_publish_date, array('use_month_names'=>true)) ?><br>
                    @ <?php echo  select_time($news_publish_date) ?>
                </fieldset>                
            </td>
        </tr>
    </table>
</div>
<?php echo  end_form_tag() ?>

<?php if ($news->is_loaded()) { ?>
<div class='div-delete' style='margin-top:5px;'>
<?php echo  form_tag('delete') ?>
<?php echo  hidden_field_tag('id',$news->get('id')) ?>
<?php echo  hidden_field_tag('action','delete') ?>
<span style='color:red;font-weight:bold'>Delete This Post?</span>
<?php echo  submit_tag('Delete Post',array('confirm'=>'Are you sure you want to delete this post?','class'=>'submit_delete')) ?>
<?php echo  end_form_tag() ?>
</div>
<br />
<h2  id='bottom_news_preview'>News Preview [ <a href="#" onclick="new Effect.ScrollTo('top_edit');return false;">back to edit&uarr;</a> ]</h2>
<div class='section'>
    <iframe 
              frameborder="0" 
              id="page_preview" 
              scrolling="auto" 
              src="<?php echo get_installed_path().'/news/preview/'.$news->get('index_name')?>"
              width="99%" 
              height="500" style='border:1px solid #000'></iframe>
</div>


<?php } ?>


<?php if ($news->is_loaded() && $news_item['status'] == 0 ) {?>
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
    new PeriodicalExecuter(function() {
                var url    = '/admin/news/modify'
                if($F('news_title') == "") return;
                tinyMCE.triggerSave();
                var params = Form.serialize($('news_authoring'))+'&commit=Save'
                new Ajax.Updater('autosave',
                                  url,
                                  {
                                      parameters: params,
                                      evalScripts:true,
                                  }
                                );
            },
        60)
// ]]>
</script>
<?php } ?>